//
//  SNStarView.m
//  star_test
//
//  Created by stone on 16/9/21.
//  Copyright © 2016年 stone. All rights reserved.
//

#import "SNStarView.h"
#define HexRGBA(rgbValue, al) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16)) / 255.0 green:((float)((rgbValue & 0xFF00) >> 8)) / 255.0 blue:((float)(rgbValue & 0xFF)) / 255.0 alpha:al]

#define HexRGB(rgbValue) HexRGBA(rgbValue, 1.0)

//=****** add class ****************** stone ***
@interface SNButton : UIButton

@end

@implementation SNButton

- (void)setHighlighted:(BOOL)highlighted {
}

@end
//=************************ stone ***
@interface SNStarView ()
/** starButtons */
@property(nonatomic, strong) NSMutableArray<SNButton *> * starButtons;
/** self.rects */
@property(nonatomic, strong) NSMutableArray<NSValue *> * rects;

@end

@implementation SNStarView

/** rects 懒加载 */
- (NSMutableArray<NSValue *> *)rects {
    if (_rects == nil) {
        _rects = [NSMutableArray array];
    }
    return _rects;
}
/** starButtons 懒加载 */
- (NSMutableArray<SNButton *> *)starButtons {
    if (_starButtons == nil) {
        _starButtons = [NSMutableArray array];
    }
    return _starButtons;
}
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panActived:)];

        [self addGestureRecognizer:pan];

        for (int i = 0; i < 5; i++) {

            SNButton * starButton = [SNButton buttonWithType:UIButtonTypeCustom];

            starButton.tag = i;

            starButton.frame = CGRectMake((19 + 21) * i, 1, 35, 19);

            [starButton addTarget:self action:@selector(starButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
            [starButton setImage:[UIImage imageNamed:@"icon_dynamic_emptystart"] forState:UIControlStateNormal];
            [starButton setImage:[UIImage imageNamed:@"icon_dynamic_truestart"] forState:UIControlStateSelected];
            if (i == 0) {
                starButton.selected = YES;
            }
            [self.starButtons addObject:starButton];

            [self addSubview:starButton];

            dispatch_async(dispatch_get_main_queue(), ^{

              //                CGRect rect = [self.navigationItem.titleView convertRect:starButton.frame toView:self.navigationController.navigationBar];

              //          UIView * view = [[UIView alloc] init];
              //
              //          view.backgroundColor = [UIColor blackColor];
              //          view.frame = rect;
              //
              //          [self.bgView addSubview:view];
              //
              [self.rects addObject:[NSValue valueWithCGRect:starButton.frame]];
              NSLog(@"%@", self.rects);
            });
        }
    }
    return self;
}
//                              /*拖拽 UIPanGestureRecognizer*/
/*****************************************************************************************/
static NSInteger indexx = 0;
- (void)panActived:(UIPanGestureRecognizer *)sender {

    //sn_note:========= ============================ stone 🐳 ===========/
    CGPoint currentPoint = [sender locationInView:sender.view];

    [self.rects enumerateObjectsUsingBlock:^(NSValue * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
      CGRect rect = [obj CGRectValue];
      BOOL flag = CGRectContainsPoint(rect, currentPoint);
      if (flag) {
          NSLog(@"%@ %ld", @"包含", idx);
          //          [self starButtonClicked:self.starButtons[idx]];
          [self.starButtons enumerateObjectsUsingBlock:^(SNButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            obj.selected = NO;
          }];

          for (int i = 0; i < idx + 1; i++) {
              self.starButtons[i].selected = YES;
              indexx = i;
          }
      }
    }];

    if (sender.state == UIGestureRecognizerStateBegan) {

    } else if (sender.state == UIGestureRecognizerStateChanged) {

    } else if (sender.state == UIGestureRecognizerStateEnded) {
        //
        __block NSInteger temp = 0;
        [self.starButtons enumerateObjectsUsingBlock:^(SNButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
          if (obj.selected) {
              temp++;
          }

        }];
        indexx = temp - 1;

        SNButton * btn = self.starButtons[indexx];
        btn.tag = indexx;
        [self starAnimation:btn];
    }
}
- (void)starAnimation:(SNButton *)sender {
    UILabel * numberLabel = [[UILabel alloc] init];
    numberLabel.numberOfLines = 0;
    numberLabel.textColor = HexRGB(0xFDAF08);
    numberLabel.textAlignment = NSTextAlignmentCenter;
    numberLabel.text = [NSString stringWithFormat:@"%ld", sender.tag + 1];
    numberLabel.font = [UIFont systemFontOfSize:9];

    [numberLabel sizeToFit];
    numberLabel.center = CGPointMake(sender.bounds.size.width * 0.5, -5);
    [sender addSubview:numberLabel];

    [UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
      //
      numberLabel.center = CGPointMake(sender.bounds.size.width * 0.5, -30);
      numberLabel.alpha = 0;
    }
        completion:^(BOOL finished) {
          //
          [numberLabel removeFromSuperview];
        }];
}

- (void)starButtonClicked:(SNButton *)sender {

    [self.starButtons enumerateObjectsUsingBlock:^(SNButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
      obj.selected = NO;
    }];

    for (int i = 0; i < sender.tag + 1; i++) {
        self.starButtons[i].selected = YES;
    }

    [self starAnimation:sender];
}
@end
